package com.globallogic.model.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import com.globallogic.model.entity.User;
import com.globallogic.model.transferobject.UserTo;

@Stateless
public class UserDao extends CommonDaoImpl<User, UserTo> {

	public UserDao() {
		clazz = User.class;
	};

	public User getByName(String name) {
		
		User user = null;
		List<Object> results = null;

		Query userQuery = em.createQuery("Select u from User u where name = :name");
		userQuery.setParameter("name", name);

		results = userQuery.getResultList();
		if (results.size() > 0) {
			user = (User) userQuery.getResultList().get(0);
		}
		
		return user;

	}
	
	public boolean isUserExists(String name){
	    return (getByName(name) != null);	
	}
	
	public boolean delete(String name) {
		User user = getByName(name);
		return delete(user.getId());
	}
}
